/**
 * 系统设置控制器
 */
import './index.less'
import $_fn from '~plugins'
export default ['$scope','$rootScope','$location','$ui','http','$state','fun','$stateParams',function($scope,$rootScope,$location,$ui,http,$state,fun,$stateParams) {
    console.log(jQuery)
    $scope.tabMenu = $_fn.getCurrMenu($location.path())
    console.log($scope.tabMenu)
    $scope.subMenu = $scope.tabMenu.children[0].children
    console.log($location.path())
	console.log('系统设置权限设置添加管理组控制器')
	/*fun.loadScript('plugins/lib/treeview/jquery.cookie.js',cookieCallback);
	function cookieCallback(){ 
		$('head').children('script:last').remove();
        fun.loadScript('plugins/lib/treeview/jquery.treeview.js',treeviewCallback);  
	} 
	function treeviewCallback(){
		$('head').children('script:last').remove();
		//sel();
	}*/
	//返回
	$scope.fanHui=function(){
		window.history.go(-1)
	}
	//权限html结构函数
	var html=[];
	var htmlbtn=function(content){
		$.each(content, function(i,t) {
			html.push('<li ><input  type=\"checkbox\" name=\"ch_id\" '+t.isChecked+' value=\"'+t.id+'\"/>'+t.name);
			if(!$_fn.isNull(t.children)) {
				html.push('<ul>');
				htmlbtn(t.children);
				html.push('</ul>');
			}
			html.push('</li>');
		});
	}
	//权限地图请求
	$scope.quanXian=function(datas,url){
		http.post(url,datas,function (data) {
			$scope.$apply(function(){
				htmlbtn(data.data);
				$('#tree').html(html.join(' '));
				console.log($('#tree'));
				console.log(html);
				console.log(html.join(' '))
				$(':checkbox').click(function () {
					if($(this).prop('checked')){
						$(this).parent().find(':checkbox').prop('checked',true);
					}else{
						$(this).parent().find(':checkbox').removeAttr('checked');
					}
					if($(this).prop('checked')) {
						$(this).parents().children(':checkbox').prop('checked', true);
					}
				});
				(function() {
					$('#tree').treeview({
						collapsed: true,
						animated: 'medium',
						control:'#sidetreecontrol',
						persist: 'location'
					});
				})()
			});
            console.log(data);
            $scope.datas=data.data
            $scope.$apply()
        },function(_data){
            console.log(_data);//报错
        })
	}
	$scope.quanXian1=function(datas,url){
		http.post(url,datas,function (data) {
			$scope.$apply(function(){
				$scope.param.role_name=data.data.role.roleName;
				$scope.param.description=data.data.role.description;
				console.log($scope.param)
				$scope.param.id=data.data.role.id;
				htmlbtn(data.data.rights);
				$('#tree').html(html.join(' '));
				$(':checkbox').click(function () {
					if($(this).prop('checked')){
						$(this).parent().find(':checkbox').prop('checked',true);
					}else{
						$(this).parent().find(':checkbox').removeAttr('checked');
					}
					if($(this).prop('checked')) {
						$(this).parents().children(':checkbox').prop('checked', true);
					}
				});
				(function() {
					$('#tree').treeview({
						collapsed: true,
						animated: 'medium',
						control:'#sidetreecontrol',
						persist: 'location'
					});
				})()
			});
            console.log(data);
            $scope.datas=data.data
            $scope.$apply()
        },function(_data){
            console.log(_data);//报错
        })
	}
	//判断是新增还是编辑
	$scope.state=$stateParams.state
	//初始化对象
	$scope.params={}
	$scope.param={}
	if($scope.state==1){
		console.log('编辑管理组')
		//获取编辑的管理组id
		$scope.params.role_id=$stateParams.id
		$scope.quanXian1($scope.params,'toUpdateRole.do')
	}else{
		console.log('新增管理组')
		$scope.quanXian({},'toInsertRole.do')
	}
	//添加或编辑管理组
	$scope.tiJiao=function(){
		$scope.param.right_ids=$_fn._fromCheckValue('ch_id');
		console.log($scope.param)
		http.post('insertRole.do',$scope.param,function (data) {
			if(data.errcode==1){
				$ui.toast({
					text:data.errmsg,
					type:'succeed'
				})
				$scope.fanHui()
			}else{
				$ui.toast({
					text:data.errmsg,
					type:'warning'
				})
			}
        },function(_data){
            console.log(_data);//报错
      	})
		
	}
	$scope.tiJiao1=function(){
		$scope.param.right_ids=$_fn._fromCheckValue('ch_id');
		console.log($scope.param)
		http.post('updateRole.do',$scope.param,function (data) {
			if(data.errcode==1){
				$ui.toast({
					text:data.errmsg,
					type:'succeed'
				})
				$scope.fanHui()
			}else{
				$ui.toast({
					text:data.errmsg,
					type:'warning'
				})
			}
        },function(_data){
            console.log(_data);//报错
      	})
		
	}
	$scope.operation = function (path,) {
        var _path = path.split('@')
        if(_path[0] == 'link'){
            console.log(path)
            console.log(_path[1])
            $state.go('setup.'+_path[1],{id:1,ids:2})
        }
        if(_path[0] == 'alert'){
        	console.log(_path[1])
            $ui.modal({content:"<"+_path[1]+"></"+_path[1]+">"})
        }
   }
}]
